//
//  RMParser.m
//  rmplanner
//
//  Created by Andrew Pigram on 10/07/12.
//  Copyright (c) 2012 PiAMP Productions. All rights reserved.
//

#import "RMParser.h"
#import "RMMeal.h"
#import "RMFirstViewController.h"
#import "RMSecondViewController.h"
#import "RMDetailViewController.h"
#import "RMAllergyViewController.h"

@interface RMParser ()

@property (strong, nonatomic) NSMutableArray* allergies;
@property (strong, nonatomic) RMCell* cell;
@property (strong, nonatomic) NSMutableArray* dates;

@end

static BOOL specialParse = NO;

@implementation RMParser

@synthesize parsing, metaData, currentMeal, xmlData, meals, xmlSender, dayArray, allergies, allergy, cell, selected, ids, dates;

#pragma mark RMParser main/set-up functions

- (void)setData:(NSData *)data sender:(UIViewController *)sender
{
	self.xmlData = data;
	self.xmlSender = sender;
	meals = [[NSMutableArray alloc] init];
	metaData = [[NSMutableString alloc] init];
	dates = [[NSMutableArray alloc] init];
}

- (void)main
{
	specialParse = NO;
	NSXMLParser *parser = [[NSXMLParser alloc] initWithData:xmlData];
	[parser setDelegate:self];
	[parser parse];
}

#pragma mark Allergy parsing functions

- (void)specialParse:(NSData *)data controller:(RMDetailViewController *)sender
{
	xmlSender = sender;
	NSXMLParser* parser = [[NSXMLParser alloc] initWithData:data];
	[parser setDelegate:self];
	allergies = [[NSMutableArray alloc] init];
	specialParse = YES;
	metaData = [[NSMutableString alloc] init];
	[parser parse];
	sender.allergies = allergies;
}

- (void)specialParse:(NSData *)data settings:(RMAllergyViewController *)sender
{
	xmlSender = sender;
	selected = [NSMutableArray array];
	NSXMLParser* parser = [[NSXMLParser alloc] initWithData:data];
	[parser setDelegate:self];
	allergies = [[NSMutableArray alloc] init];
	specialParse = YES;
	metaData = [[NSMutableString alloc] init];
	ids = [[NSMutableArray alloc] init];
	[parser parse];
	sender.allergies = allergies;
	sender.selected = selected;
	sender.ids = ids;
}

#pragma mark NSXMLParser delegate functions

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
	// place parsing code here
	if ([elementName isEqualToString:@"meal"])
	{
		// initialise a new meal object
		currentMeal = [[RMMeal alloc] init];
	}
	else if ([elementName isEqualToString:@"day"])
	{
		dayArray = [[NSMutableArray alloc] init];
	}
	else if ([elementName isEqualToString:@"name"]
			 || [elementName isEqualToString:@"description"]
			 || [elementName isEqualToString:@"date"]
			 || [elementName isEqualToString:@"id"]
			 || [elementName isEqualToString:@"selected"]
			 || [elementName isEqualToString:@"voted"]
			 || [elementName isEqualToString:@"ordered"]
			 || [elementName isEqualToString:@"orderable"])
	{
		// mark data string for filling, nullifying it if it has been previously used
		parsing = YES;
		[metaData setString:@""];
	}
}

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
	// place parsing code here
	if ([elementName isEqualToString:@"meal"])
	{
		// add completed meal to the list
		if ([xmlSender.navigationItem.title isEqualToString:@"Today's Meals"])
			[self.meals addObject:currentMeal];
		else
			[dayArray addObject:currentMeal];
		currentMeal = nil;
	}
	else if ([elementName isEqualToString:@"allergy"])
	{
		[allergies addObject:allergy];
	}
	else if ([elementName isEqualToString:@"day"])
	{
		[meals addObject:dayArray];
		dayArray = nil;
	}
	else if ([elementName isEqualToString:@"name"])
	{
		// assign the string to the name field
		if (!specialParse)
			currentMeal.name = [NSString stringWithString:metaData];
		else
			allergy = [NSString stringWithString:metaData];
	}
	else if ([elementName isEqualToString:@"selected"])
	{
		NSInteger select = 0;
		NSScanner* scanner = [[NSScanner alloc] initWithString:metaData];
		[scanner scanInteger:&select];
		[selected addObject:[NSNumber numberWithInt:select]];
	}
	else if ([elementName isEqualToString:@"description"])
	{
		// assign the string to the description field
		currentMeal.description = [NSString stringWithString:metaData];
	}
	else if ([elementName isEqualToString:@"date"])
	{
		// parse a date from the string and assign it to the date field
		[dates addObject:[NSString stringWithString:metaData]];
	}
	else if ([elementName isEqualToString:@"id"])
	{
		if (!specialParse)
		{
			NSScanner* scanner = [NSScanner scannerWithString:metaData];
			int value = 0;
			[scanner scanInt:&value];
			currentMeal.mealID = value;
		}
		else
		{
			NSScanner* scanner = [NSScanner scannerWithString:metaData];
			int value = 0;
			[scanner scanInt:&value];
			[ids addObject:[NSNumber numberWithInt:value]];
		}
	}
	else if ([elementName isEqualToString:@"voted"])
	{
		NSScanner* scanner = [NSScanner scannerWithString:metaData];
		NSInteger value = 0;
		[scanner scanInt:&value];
		currentMeal.voted = value;
	}
	else if ([elementName isEqualToString:@"ordered"])
	{
		NSScanner* scanner = [NSScanner scannerWithString:metaData];
		NSInteger value = 0;
		[scanner scanInt:&value];
		currentMeal.ordered = value;
	}
	else if ([elementName isEqualToString:@"orderable"])
	{
		NSScanner* scanner = [NSScanner scannerWithString:metaData];
		int value = 0;
		[scanner scanInt:&value];
		currentMeal.orderable = value;
	}
	parsing = NO;
}

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
	if (parsing)
		[metaData appendString:string];
}

- (void)parserDidEndDocument:(NSXMLParser *)parser
{
	if (!specialParse)
	{
		// when the document has ended, send the formed array to the appropriate view controller
		if ([xmlSender.navigationItem.title isEqualToString:@"Today's Meals"])
		{
			RMFirstViewController* controller = (RMFirstViewController *)xmlSender;
			controller.meals = meals;
			controller.buttonStates = [[NSMutableArray alloc] initWithCapacity:[controller.meals count]];
			for (int i = 0; i < [controller.meals count]; i++)
			{
				[controller.buttonStates addObject:[NSNumber numberWithInt:0]];
			}
			[controller.tableView reloadData];
		}
		else
		{
			RMSecondViewController* controller = (RMSecondViewController *)xmlSender;
			controller.meals = meals;
			controller.dates = dates;
			for (int i = 0; i < [controller.meals count]; i++)
			{
				[controller.sectionOpen addObject:[NSNumber numberWithInt:0]];
			}
			[controller.tableView reloadData];
		}
	}
}

@end
